package com.teamwork.ZuccEcho.Controller;

import com.alibaba.fastjson.JSON;
import com.teamwork.ZuccEcho.Entity.Course;
import com.teamwork.ZuccEcho.Entity.Info;
import com.teamwork.ZuccEcho.Entity.Student;
import com.teamwork.ZuccEcho.Repository.ClassApiRepository;
import com.teamwork.ZuccEcho.Repository.InformationApiRepository;
import com.teamwork.ZuccEcho.Result.ExceptionMsg;
import com.teamwork.ZuccEcho.Result.Response;
import com.teamwork.ZuccEcho.Result.ResponseData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/info")
public class InformationController {
    List<Info> infos=new ArrayList<>();//存放系统通知
    @Autowired
    private InformationApiRepository informationApiRepository;
    @Autowired
    private ClassApiRepository classApiRepository;

    protected Response result(ExceptionMsg msg){
        return new Response(msg);
    }

    @RequestMapping(value = "/publish/v1/", method = RequestMethod.POST)
    @ResponseBody
    public ResponseData publishInfo(@RequestBody Map<String,String> body){
        long course_id= Long.parseLong(body.get("courseid"));
        String info_title=body.get("title");
        String info_content=body.get("content");
        //List<Student> students=informationApiRepository.findStudentsByCourseId(course_id);
        Course course = classApiRepository.findById(course_id);

        Info info=new Info();
        info.setCourse(course);
        info.setI_title(info_title);
        info.setI_content(info_content);
        info.setI_publishdate(new Date());

        informationApiRepository.save(info);
        return new ResponseData(ExceptionMsg.SUCCESS,info.toString());
    }

    @RequestMapping(value = "/getinfo/v1/", method = RequestMethod.GET)
    @ResponseBody
    public ResponseData searchStudentInfo(@RequestBody Map<String,String> body){
        String stu_id= body.get("stu_id");
        List<Course> courses =classApiRepository.getCoursesByStu(stu_id);
        if(courses.size()>0){
            String strJSON = JSON.toJSONString(courses.toString());
            return new ResponseData(ExceptionMsg.SUCCESS,strJSON);
        }else {
            return new ResponseData(ExceptionMsg.FAILED,"该学生没有通知");
        }
    }


}
